﻿using SALESolution.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SALESolution.Controllers
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            ShoppingCartModels model = new ShoppingCartModels();
            model.Cart = (ShoppingCart)Session["Cart"];
            return View(model);
        }

        /// <summary>
        /// Them san pham vao gio hang
        /// </summary>
        /// <param name="id">ID san pham can them</param>
        /// <returns>Tra ve json theo dinh dang {Code: ReturnCode, Msg: "Return message"}</returns>
        [HttpPost]
        public JsonResult AddToCart(long id)
        {
            var response = new { Code = 1, Msg = "Fail" };
            Product objProduct = GetProductByID(id);
            if (objProduct != null)
            {
                ShoppingCart objCart = (ShoppingCart)Session["Cart"];
                if (objCart == null)
                {
                    objCart = new ShoppingCart();
                }

                ShoppingCartItem item = new ShoppingCartItem()
                {
                    ProductID = objProduct.ProductID,
                    ProductName = objProduct.ProductName,
                    ProductImage = objProduct.ProductImage,
                    Price = objProduct.Price,
                    Quantity = 1,
                    Total = objProduct.Price,
                };

                objCart.AddToCart(item);
                Session["Cart"] = objCart;
                response = new { Code = 0, Msg = "Success" };
            }
            return Json(response);
        }

        /// <summary>
        /// Xoa san pham khoi gio hang
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveFromCart(long id)
        {
            var response = new { Code = 1, Msg = "Fail" };

            ShoppingCart objCart = (ShoppingCart)Session["Cart"];
            if (objCart != null)
            {
                objCart.RemoveFromCart(id);
                Session["Cart"] = objCart;
                response = new { Code = 0, Msg = "Success" };
            }
            return Json(response);
        }

        /// <summary>
        /// Cap nhat so luong san pham can mua trong gio hang
        /// </summary>
        /// <param name="id"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateQuantity(long id, int quantity)
        {
            var response = new { Code = 1, Msg = "Fail" };

            ShoppingCart objCart = (ShoppingCart)Session["Cart"];
            if (objCart != null)
            {
                objCart.UpdateQuantity(id, quantity);
                Session["Cart"] = objCart;
                response = new { Code = 0, Msg = "Success" };
            }
            return Json(response);
        }

        /// <summary>
        /// Gia lap method lay thong tin san pham tu DB dua vao ID san pham
        /// </summary>
        /// <param name="id">ID san pham can lay thong tin</param>
        /// <returns></returns>
        public Product GetProductByID(long id)
        {
            return new Product()
            {
                ProductID = id,
                ProductName = String.Format("Product {0}", id),
                ProductImage = String.Format("/images/hinh{0}.png", id),
                Price = 10000 * id
            };
        }

    }
}
